package com.greenova.projector.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.greenova.projector.entity.Person;
import com.greenova.projector.entity.Project;
import com.greenova.projector.model.PersonInputModel;
import com.greenova.projector.model.ProjectInputModel;
import com.greenova.projector.service.PersonService;
import com.greenova.projector.service.ProjectService;

@Controller
public class ProjectsController {

	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private PersonService personService;

	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public ModelAndView home(ModelAndView modelAndView,
			@ModelAttribute ProjectInputModel assignProjectInputModel) {
		List<Project> projects = projectService.listAllProjects();
		modelAndView.addObject("projects", projects);
		int projectSize = projects.size();
		modelAndView.addObject("projectSize", projectSize);
		modelAndView.setViewName("home");
		return modelAndView;
	}

	@RequestMapping("/createProject")
	public ModelAndView helloWorld(ModelAndView modelAndView) {
		modelAndView.addObject("projectInputModel",
				new ProjectInputModel());
		modelAndView.setViewName("create-project");
		return modelAndView;
	}

	@RequestMapping(value = "/saveProject", method = RequestMethod.POST)
	public ModelAndView saveProject(ModelAndView modelAndView,
			@ModelAttribute ProjectInputModel assignProjectInputModel) {
		Project project = mapToProject(assignProjectInputModel);
		projectService.addProject(project);
		
		List<Project> projects = projectService.listAllProjects();
		modelAndView.addObject("projects", projects);
		
	
		modelAndView.setViewName("home");
		
		return modelAndView;
	}
	
	@RequestMapping(value = "/projectAssignments", method = RequestMethod.POST)
	public ModelAndView projectAssignments(ModelAndView modelAndView, HttpServletRequest request) {
		Long projectId = Long.parseLong(request.getParameter("selectedProject"));
		
		Set<Person> personsNotAssignedInProject = personService.listAllPersonsNotAssignedInProject(projectId);
		modelAndView.addObject("personsNotAssigned", personsNotAssignedInProject);
		
		Set<Person> personsAssignedInProject = personService.listAllPersonsAssignedInProject(projectId);
		modelAndView.addObject("personsAssigned", personsAssignedInProject);
		
		modelAndView.addObject("projectId", projectId);
		
		modelAndView.setViewName("project-assignments");
		
		return modelAndView;
	}
	
	@RequestMapping(value = "/assignPerson", method = RequestMethod.POST)
	public ModelAndView assignPerson(ModelAndView modelAndView, HttpServletRequest request) {
		
		long projectId = Long.parseLong(request.getParameter("projectId"));
		
		Project project = projectService.getProjectById(projectId);
		String personsNotAssigned = request.getParameter("personThatIsNotAssigned");
		Person person = personService.getPersonById(Long.parseLong(personsNotAssigned));
		
		Set<Person> persons = new HashSet<Person>();
		persons.add(person);
		
		for(Person personInProject : project.getPersons()) {
			persons.add(personInProject);
		}
		
		project.setPersons(persons);
		projectService.updateProject(project);
		
		Set<Person> personsNotAssignedInProject = personService.listAllPersonsNotAssignedInProject(projectId);
		modelAndView.addObject("personsNotAssigned", personsNotAssignedInProject);
		
		Set<Person> personsAssignedInProject = personService.listAllPersonsAssignedInProject(projectId);
		modelAndView.addObject("personsAssigned", personsAssignedInProject);
		
		modelAndView.addObject("projectId", projectId);
		
		modelAndView.setViewName("project-assignments");
		//modelAndView.setViewName("hello");
		return modelAndView;
		
		
		
	}
	
	@RequestMapping(value = "/unassignPerson", method = RequestMethod.POST)
	public ModelAndView unassignPerson(ModelAndView modelAndView, HttpServletRequest request) {
		long projectId = Long.parseLong(request.getParameter("projectId"));
		
		Project project = projectService.getProjectById(projectId);
		String personUnassigned = request.getParameter("unassignPersonInProject");

		Set<Person> persons = new HashSet<Person>();
		
		for(Person personInProject : project.getPersons()) {
			persons.add(personInProject);
		}
		Person person = personService.getPersonById(Long.parseLong(personUnassigned));
		
		persons.remove(person);
		
		project.setPersons(persons);
		projectService.updateProject(project);
		
		Set<Person> personsNotAssignedInProject = personService.listAllPersonsNotAssignedInProject(projectId);
		modelAndView.addObject("personsNotAssigned", personsNotAssignedInProject);
		
		Set<Person> personsAssignedInProject = personService.listAllPersonsAssignedInProject(projectId);
		modelAndView.addObject("personsAssigned", personsAssignedInProject);
		
		modelAndView.addObject("projectId", projectId);
		
		modelAndView.setViewName("project-assignments");
		//modelAndView.setViewName("hello");
		return modelAndView;
		
	}
	

	private Project mapToProject(ProjectInputModel assignProjectInputModel) {
		Project project = new Project();
		project.setCode(assignProjectInputModel.getCode());
		project.setName(assignProjectInputModel.getName());
		project.setBudget(assignProjectInputModel.getBudget());
		project.setRemarks(assignProjectInputModel.getRemarks());
		return project;
	}
}
